package com.duomai.myapplication;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.UserDao;
import com.example.admin.base.BaseActivity;

import java.util.ArrayList;

public class RegisterActivity extends BaseActivity {
    EditText tvName, tvPass,tvrePass;
    TextView tvAddre;
    Spinner spinner1;
    UserDao userDao;
    @Override
    public void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        userDao=new UserDao(this);
        tvName = findViewById(R.id.tvName);
        spinner1 = findViewById(R.id.spinner1);
        tvPass = findViewById(R.id.tvPass);
        tvrePass = findViewById(R.id.tvrePass);
        tvAddre = findViewById(R.id.tvAddre);
    }

    @Override
    public void setInitView() {

        findViewById(R.id.tv_login).setOnClickListener(v -> {
            String name = tvName.getText().toString().trim();
            String pass = tvPass.getText().toString().trim();
            String repass = tvrePass.getText().toString().trim();
            String addre = (String) spinner1.getSelectedItem();
            if (TextUtils.isEmpty(name)){
                toast("输入账号");
                return;
            }
            if (!TextUtils.equals(pass,repass)){
                toast("密码不一致");
                return;
            }
            ArrayList<User> list =  userDao.findWhereUser(name);
            if (list.size() == 0) {
                User user = new User();
                user.userName = name;
                user.UserPass = pass;
                user.UserAddress = addre;
                userDao.addUserData(user);
                toast("注册成功");
finish();
            } else {
                toast("账号已存在");

            }

        });
    }
}